import React, { Component } from 'react';
import axios from 'axios'
import PubSub from 'pubsub-js'
import { Form, Input, Button } from 'antd';
import 'antd/dist/antd.css'

const Demo = (e) => {
  const onFinish = (values) => {
    let params=()=>{
        if(e.type){
          return {
            params:{
              typeone:e.type,
              typetwo:values.type,
              token:localStorage.getItem("token")
            },
        }
        }else {
          return {
            params:{
              type:values.type,
              token:localStorage.getItem("token")
            },
        }
        }     
    }
    axios.get(e.add,params())
    .then((res)=>{
      console.log(res);
      if (res.data.code==1) {
        PubSub.publish('arguigu',{type:"success", name:"添加成功",display:"flex"})
        e.gengxin()
        setTimeout(()=>{
          PubSub.publish('arguigu',{display:"none"})
        },1500)
      }else{
        PubSub.publish('arguigu',{type:"error", name:"添加失败",display:"flex"})
        setTimeout(()=>{
          PubSub.publish('arguigu',{display:"none"})
        },1500)
      }
    })
  };

  const onFinishFailed = (errorInfo) => {
  };

  return (
    <Form 
      name="basic"
      labelCol={{
        span: 8,
      }}
      wrapperCol={{
        span: 16,
      }}
      initialValues={{
        remember: true,
      }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
    >
      <Form.Item
        label="分类名"
        name="type"
        rules={[
          {
            required: true,
            message: '请输入分类名',
          },
        ]}
      >
        <Input />
      </Form.Item>

      <Form.Item
        wrapperCol={{
          offset: 8,
          span: 16,
        }}
      >
        <Button type="primary" htmlType="submit">
          确认
        </Button>
      </Form.Item>
    </Form>
  );
};
class index extends Component {

  render() {
    const { name, display,  gengxin ,add,type} = this.props
    return (
      <div>
        <Demo type={type} add={add} gengxin={gengxin} />
        {/* <App gengxin={gengxin} url={url} display={display} name={name}/> */}
      </div>
    );
  }
}

export default index;